﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Face.Web.Message
{

    public partial class MessageReceive : System.Web.UI.Page
    {

        private static IBLL.IMessage bllVideo = BLLFactory.DataAccess.getMessage();
        private static IBLL.IMessagesInVideoDisplay bll = BLLFactory.DataAccess.CreateMessagesInVideoDisplay();
        public int ThisVideoID;
        public string UserName;

        protected void btn_logout(object sender, EventArgs e)
        {
            Session.Remove("user");
            Response.Redirect("/login_and_signup/login.aspx");
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["VideoID"] != null)
            {
                int test;
                if (System.Int32.TryParse(Request.QueryString["VideoID"], out test))
                    ThisVideoID = test;
            }
            if (Session["user"] == null)
            {
                Response.Redirect("/login_and_signup/login.aspx");
            }
            else
            {
                UserName = Session["user"].ToString();
            }
            if (!IsPostBack)
            {
                bindData();
            }
        }

        private void bindData()
        {
            Message_info.DataSource = bllVideo.GetReceiveMessages(UserName);
            Message_info.DataBind();
        }

        protected void rpt_Message_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "btn_Delete")
            {
                bll.DeleteMessageByMessageID(System.Int32.Parse(e.CommandArgument.ToString()));
                bindData();
            }
            if (e.CommandName == "btn_ReceiveOK")
            {
                string kindID = e.CommandArgument.ToString().Split(',')[0];
                int itemIndex = Convert.ToInt32(e.CommandArgument.ToString().Split(',')[1]);
                System.Web.UI.HtmlControls.HtmlInputText inputText = this.Message_info.Items[itemIndex].FindControl("text_Rename") as System.Web.UI.HtmlControls.HtmlInputText;
                DataMode.message msg = new DataMode.message();
                DataMode.message goalmsg = bllVideo.GetMessageByMessageID(System.Int32.Parse(kindID));
                msg.username = UserName;
                msg.message_time = DateTime.Now;
                msg.message_content = inputText.Value;

                msg.reply_id = bll.GetUserIDByUserName(goalmsg.username);
                msg.reply_username = goalmsg.username;
                msg.video_id = ThisVideoID;
                //调用业务层处理
                bll.InsertMessage(msg);
                inputText.Value = "";
                bindData();
            }
        }
    }
}